package com.xmy.cultivate.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.service.AuthorityServic;
import com.xmy.cultivate.service.impl.AuthorityImpl;
import com.xmy.cultivate.entity.response.UserSort;
import com.xmy.cultivate.mapper.RecordCourseMapper;
import com.xmy.cultivate.mapper.StaffMapper;
import com.xmy.cultivate.service.ICourseSchedulingService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/courseScheduling")
public class CourseSchedulingController extends BaseController{

    @Autowired
    @Lazy
    ICourseSchedulingService iCourseSchedulingService;

    @Autowired
    @Lazy
    RecordCourseMapper recordCourseMapper;

    @Autowired
    @Lazy
    StaffMapper staffMapper;

    @Autowired
    @Lazy
    AuthorityServic authorityServic;

    @GetMapping("selectCurriculum")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "teacherId" , value = "老师id" ,required = true ,dataType = "String"),
            @ApiImplicitParam(name = "changeDate" , value = "所选日期",required = false ,dataType = "String"),
            @ApiImplicitParam(name = "gradeId", value = "班级id",required = false ,dataType = "String"),
            @ApiImplicitParam(name = "schoolId", value = "校区id",required = false,dataType = "String")
    })
    @ApiOperation("获取课表")
    public Result selectCurriculum(@RequestParam Map reMap){
        String teacherId = String.valueOf(this.adminId);
        String changeDate = (String) reMap.get("changeDate");
        String gradeId = (String) reMap.get("gradeId");
        String schoolId = (String) reMap.get("schoolId");
        Staff staff = staffMapper.selectByAdminId(Long.valueOf(teacherId));
        int laber = authorityServic.get(teacherId);  //设置标志位
        //标志位1为普通老师，标志位2为校长，标志位3为财务或者主管

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtils.isNotBlank(gradeId),"grade_id",gradeId);
        queryWrapper.eq("deleted",0);
        if(StringUtils.isNotBlank(changeDate)){
            queryWrapper.eq( "school_date",changeDate);
        }else{
            queryWrapper.eq("school_date", LocalDate.now());
        }
        queryWrapper.groupBy("teacher_id","time_start","time_end");

        if(laber == 1){
            queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
            queryWrapper.eq("teacher_id",teacherId);
        }
        //如果是校长则可以查询管辖下所有校区
        if(laber == 2){
            if(staff.getSchoolId().equals(schoolId) || schoolId == null){
                queryWrapper.eq("school_id",staff.getSchoolId());
                queryWrapper.ne("teacher_id",teacherId);
            }
        }
        //如果是主管和财务则可以查找所选的全部校区
        if(laber == 3){
            queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        }
        List<CourseSchedulingDetail> courseSchedulingDetailList = iCourseSchedulingService.selectCurriculum(queryWrapper);
        if(laber == 2){
            queryWrapper.clear();
            queryWrapper.eq("teacher_id",teacherId);
            queryWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
            List<CourseSchedulingDetail> courseSchedulingDetailListNext = iCourseSchedulingService.selectCurriculum(queryWrapper);
            courseSchedulingDetailList.addAll(courseSchedulingDetailListNext);
        }
        UserSort.sortByCourse(courseSchedulingDetailList);
        return new Result(Code.OK,courseSchedulingDetailList,"获取课表成功~");
    }
}
